import json
import requests
from lxml import etree

base_url = "https://www.hbjcxy.com/jyyw/index" #.html
for i in range(0,5):
    if i==0:
        url = base_url+".html"
        # 请求网页
        response = requests.get(url)
        # 设置编码方式
        response.encoding = "utf-8"
        # 解析网页
        node = etree.HTML(response.text)
        # titles
        titles = node.xpath("//div[@class='list-item']/h4/a/text()")
        # 日期
        dates = node.xpath("//div[@class='list-item']/div/text()")
        items = []
        for j in range(len(titles)):
            d = {}
            d["标题"] = str(titles[j]).strip()
            d["时间"] = str(dates[j]).strip()
            items.append(d)
        print(items)
        with open("标题和日期/"+str(i)+".json","w+",encoding="utf-8") as f:
            f.write(json.dumps(items,ensure_ascii=False,indent=2))
    else:
        url = base_url+"_"+str(i+1)+".html"
        # 请求网页
        response = requests.get(url)
        # 设置编码方式
        response.encoding = "utf-8"
        # 解析网页
        node = etree.HTML(response.text)
        # titles
        titles = node.xpath("//div[@class='list-item']/h4/a/text()")
        # 日期
        dates = node.xpath("//div[@class='list-item']/div/text()")
        items = []
        for j in range(len(titles)):
            d = {}
            d["标题"] = str(titles[j]).strip()
            d["时间"] = str(dates[j]).strip()
            items.append(d)
        print(items)
        with open("标题和日期/" + str(i) + ".json", "w+", encoding="utf-8") as f:
            f.write(json.dumps(items, ensure_ascii=False, indent=2))